<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="预录单编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入预录单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户类型" prop="customerType">
        <el-select v-model="queryParams.customerType" placeholder="请选择客户类型" clearable>
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="受理日期">
        <el-date-picker
          v-model="daterangeDateAccepted"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="jcOrderCode">
        <el-input
          v-model="queryParams.jcOrderCode"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['order:presco:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['order:presco:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['order:presco:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-circle-check"
          size="mini"
          @click="handleConfirm"
          v-hasPermi="['order:presco:confirm']"
        >确认</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="prescoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" fixed/>
      <el-table-column label="预录单编号" align="center" prop="code" width="100"/>
      <el-table-column label="组织机构" align="center" prop="jcOrganizationIdDtLabel"/>
      <el-table-column label="客户名称" align="center" prop="customerName" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="客户类型" align="center" prop="customerType">
        <template #default="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.customerType"/>
        </template>
      </el-table-column>
      <el-table-column label="受理日期" align="center" prop="dateAccepted" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.dateAccepted, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="派车单编号" align="center" prop="jcDispatchId" width="100"/>
      <el-table-column label="订单编号" align="center" prop="jcOrderId" width="100"/>
      <el-table-column label="发货单号" align="center" prop="relateBill1" width="100"/>
      <el-table-column label="发货单位" align="center" prop="fhName" />
      <el-table-column label="发货人" align="center" prop="fhPerson" />
      <el-table-column label="发货省市县" align="center" prop="fhAddress" />
      <el-table-column label="发货电话" align="center" prop="fhIphone" />
      <el-table-column label="发货详细地址" align="center" prop="fhDetaileaddress" width="250" :show-overflow-tooltip="true"/>
      <el-table-column label="收货单位" align="center" prop="shName" />
      <el-table-column label="收货人" align="center" prop="shPerson" />
      <el-table-column label="收货省市县" align="center" prop="shAddress" />
      <el-table-column label="收货电话" align="center" prop="shIphone" />
      <el-table-column label="收货详细地址" align="center" prop="shDetaileaddress" :show-overflow-tooltip="true"/>
      <el-table-column label="联系人" align="center" prop="contactperson" />
      <el-table-column label="联系电话" align="center" prop="iphone" />
      <el-table-column label="计重" align="center" prop="jzWeight" />
      <el-table-column label="数量" align="center" prop="number" />
      <el-table-column label="货值" align="center" prop="value" />
      <el-table-column label="体积" align="center" prop="volume" />
      <el-table-column label="重量" align="center" prop="weight" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['order:presco:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['order:presco:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户预录单对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="预录单编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入预录单编号" />
        </el-form-item>
        <el-form-item label="创建人" prop="createName">
          <el-input v-model="form.createName" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="备注" prop="description">
          <el-input v-model="form.description" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="修改人" prop="modifyName">
          <el-input v-model="form.modifyName" placeholder="请输入修改人" />
        </el-form-item>
        <el-form-item label="修改时间" prop="modifyTime">
          <el-date-picker clearable
            v-model="form.modifyTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择修改时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="${comment}" prop="name">
          <el-input v-model="form.name" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="客户类型" prop="customerType">
          <el-select v-model="form.customerType" placeholder="请选择客户类型">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="受理日期" prop="dateAccepted">
          <el-date-picker clearable
            v-model="form.dateAccepted"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择受理日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="提货公里数" prop="pickMileage">
          <el-input v-model="form.pickMileage" placeholder="请输入提货公里数" />
        </el-form-item>
        <el-form-item label="派车单" prop="jcDispatchId">
          <el-input v-model="form.jcDispatchId" placeholder="请输入派车单" />
        </el-form-item>
        <el-form-item label="组织ID" prop="jcOrganizationId">
          <el-input v-model="form.jcOrganizationId" placeholder="请输入组织ID" />
        </el-form-item>
        <el-form-item label="客户名称(零散客户)" prop="customerNameLs">
          <el-input v-model="form.customerNameLs" placeholder="请输入客户名称(零散客户)" />
        </el-form-item>
        <el-form-item label="客户ID" prop="jcCustomerId">
          <el-input v-model="form.jcCustomerId" placeholder="请输入客户ID" />
        </el-form-item>
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="发货省市县" prop="fhAddress">
          <el-input v-model="form.fhAddress" placeholder="请输入发货省市县" />
        </el-form-item>
        <el-form-item label="发货电话" prop="fhIphone">
          <el-input v-model="form.fhIphone" placeholder="请输入发货电话" />
        </el-form-item>
        <el-form-item label="发货坐标" prop="fhLtl">
          <el-input v-model="form.fhLtl" placeholder="请输入发货坐标" />
        </el-form-item>
        <el-form-item label="发货单位" prop="fhName">
          <el-input v-model="form.fhName" placeholder="请输入发货单位" />
        </el-form-item>
        <el-form-item label="发货人" prop="fhPerson">
          <el-input v-model="form.fhPerson" placeholder="请输入发货人" />
        </el-form-item>
        <el-form-item label="发货详细地址" prop="fhDetaileaddress">
          <el-input v-model="form.fhDetaileaddress" placeholder="请输入发货详细地址" />
        </el-form-item>
        <el-form-item label="收货省市县地址" prop="shAddress">
          <el-input v-model="form.shAddress" placeholder="请输入收货省市县地址" />
        </el-form-item>
        <el-form-item label="收货电话" prop="shIphone">
          <el-input v-model="form.shIphone" placeholder="请输入收货电话" />
        </el-form-item>
        <el-form-item label="收货坐标" prop="shLtl">
          <el-input v-model="form.shLtl" placeholder="请输入收货坐标" />
        </el-form-item>
        <el-form-item label="收货单位" prop="shName">
          <el-input v-model="form.shName" placeholder="请输入收货单位" />
        </el-form-item>
        <el-form-item label="收货人" prop="shPerson">
          <el-input v-model="form.shPerson" placeholder="请输入收货人" />
        </el-form-item>
        <el-form-item label="收货详细地址" prop="shDetaileaddress">
          <el-input v-model="form.shDetaileaddress" placeholder="请输入收货详细地址" />
        </el-form-item>
        <el-form-item label="发货单号" prop="relateBill1">
          <el-input v-model="form.relateBill1" placeholder="请输入发货单号" />
        </el-form-item>
        <el-form-item label="服务区域" prop="jcServerZoneId">
          <el-input v-model="form.jcServerZoneId" placeholder="请输入服务区域" />
        </el-form-item>
        <el-form-item label="联系人" prop="contactperson">
          <el-input v-model="form.contactperson" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="iphone">
          <el-input v-model="form.iphone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="计重" prop="jzWeight">
          <el-input v-model="form.jzWeight" placeholder="请输入计重" />
        </el-form-item>
        <el-form-item label="数量" prop="number">
          <el-input v-model="form.number" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="货值" prop="value">
          <el-input v-model="form.value" placeholder="请输入货值" />
        </el-form-item>
        <el-form-item label="体积" prop="volume">
          <el-input v-model="form.volume" placeholder="请输入体积" />
        </el-form-item>
        <el-form-item label="重量" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入重量" />
        </el-form-item>
        <el-form-item label="订单ID" prop="jcOrderId">
          <el-input v-model="form.jcOrderId" placeholder="请输入订单ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPresco, getPresco, delPresco, updatePresco } from "@/api/order/presco";

export default {
  name: "Presco",
  dicts: ['sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      codes: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户预录单表格数据
      prescoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 订单ID时间范围
      daterangeCreateTime: [],
      // 订单ID时间范围
      daterangeModifyTime: [],
      // 订单ID时间范围
      daterangeDateAccepted: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        status: null,
        address: null,
        customerType: null,
        dateAccepted: null,
        pickMileage: null,
        jcDispatchId: null,
        jcOrganizationId: null,
        customerNameLs: null,
        jcCustomerId: null,
        customerName: null,
        fhAddress: null,
        fhIphone: null,
        fhLtl: null,
        fhName: null,
        fhPerson: null,
        fhDetaileaddress: null,
        shAddress: null,
        shIphone: null,
        shLtl: null,
        shName: null,
        shPerson: null,
        shDetaileaddress: null,
        relateBill1: null,
        jcServerZoneId: null,
        contactperson: null,
        iphone: null,
        jzWeight: null,
        number: null,
        value: null,
        volume: null,
        weight: null,
        jcOrderId: null,
        jcOrderCode: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客户预录单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      if (null != this.daterangeModifyTime && '' != this.daterangeModifyTime) {
        this.queryParams.params["beginModifyTime"] = this.daterangeModifyTime[0];
        this.queryParams.params["endModifyTime"] = this.daterangeModifyTime[1];
      }
      if (null != this.daterangeDateAccepted && '' != this.daterangeDateAccepted) {
        this.queryParams.params["beginDateAccepted"] = this.daterangeDateAccepted[0];
        this.queryParams.params["endDateAccepted"] = this.daterangeDateAccepted[1];
      }
      listPresco(this.queryParams).then(response => {
        this.prescoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        createName: null,
        createTime: null,
        description: null,
        modifyName: null,
        modifyTime: null,
        name: null,
        status: null,
        address: null,
        customerType: null,
        dateAccepted: null,
        pickMileage: null,
        jcDispatchId: null,
        jcOrganizationId: null,
        customerNameLs: null,
        jcCustomerId: null,
        customerName: null,
        fhAddress: null,
        fhIphone: null,
        fhLtl: null,
        fhName: null,
        fhPerson: null,
        fhDetaileaddress: null,
        shAddress: null,
        shIphone: null,
        shLtl: null,
        shName: null,
        shPerson: null,
        shDetaileaddress: null,
        relateBill1: null,
        jcServerZoneId: null,
        contactperson: null,
        iphone: null,
        jzWeight: null,
        number: null,
        value: null,
        volume: null,
        weight: null,
        jcOrderId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.daterangeModifyTime = [];
      this.daterangeDateAccepted = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.codes = selection.map(item => item.code)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户预录单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPresco(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户预录单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePresco(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除客户预录单编号为"' + ids + '"的数据项？').then(function() {
        return delPresco(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('order/presco/export', {
        ...this.queryParams
      }, `presco_${new Date().getTime()}.xlsx`)
    },
    handleConfirm() {
      alert('开发中...')
    }
  }
};
</script>
